﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VHWebStudy.App_Code;
using Lucene.Net.Store;
using Lucene.Net.Analysis.Standard;
using System.IO;
using Lucene.Net.Search;
using Lucene.Net.Index;

namespace Study.VHProject新功能研究
{
    public partial class 全文索引Lucene : System.Web.UI.Page
    {
        public List<Archive> arcs = new List<Archive>();
        public int TotalCount = 0;
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnAddIndex_Click(object sender, EventArgs e)
        {
            string arcId = tbArcId.Text.Trim();
            string arcName = tbArcName.Text.Trim();
            LuceneKit.AddIndex(arcId, arcName);
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string arcId = tbArcId2.Text.Trim();
            string arcName=tbArcName2.Text.Trim();
            arcs=LuceneKit.IndexSearch(arcId, arcName);
        }

        protected void IndexDelete_Click(object sender, EventArgs e)
        {
            string arcId = tbArcId3.Text.Trim();
            LuceneKit.DeleteIndex(arcId);
        }

        protected void btnUpdateIndex_Click(object sender, EventArgs e)
        {
            string arcId = tbArcId4.Text.Trim();
            string arcName = tbArcName4.Text.Trim();
            LuceneKit.UpdateIndex(arcId, arcName);
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            int CurrentPage = 1;
            int PageSize = 30;
            string segPath = "F://123文档/Index";

            FSDirectory directory = null;
            StandardAnalyzer analyzer = null;
            IndexSearcher searcher = null;

            Lucene.Net.Util.Version ver = Lucene.Net.Util.Version.LUCENE_30;
            try
            {
                directory = FSDirectory.Open(new DirectoryInfo(segPath), new NativeFSLockFactory());
                analyzer = new StandardAnalyzer(ver);//StandardAnalyzer只支持英文的空格分词

                TopScoreDocCollector collector = TopScoreDocCollector.Create(CurrentPage * PageSize, false);
                searcher = new IndexSearcher(directory, true);
                string wildcard = "*";
                string keyWord = TextBox1.Text.Trim();

                Term t0 = new Term("ArchiveName", wildcard + keyWord + wildcard);
                WildcardQuery query0 = new WildcardQuery(t0);
                Term t1 = new Term("CustomField1", wildcard + keyWord + wildcard);
                WildcardQuery query1 = new WildcardQuery(t1);
                Term t2 = new Term("CustomField2", wildcard + keyWord + wildcard);
                WildcardQuery query2 = new WildcardQuery(t2);
                Term t3 = new Term("CustomField3", wildcard + keyWord + wildcard);
                WildcardQuery query3 = new WildcardQuery(t3);
                Term t4 = new Term("CustomField4", wildcard + keyWord + wildcard);
                WildcardQuery query4 = new WildcardQuery(t4);
                Term t5 = new Term("CustomField5", wildcard + keyWord + wildcard);
                WildcardQuery query5 = new WildcardQuery(t5);
                Term t6 = new Term("CustomField6", wildcard + keyWord + wildcard);
                WildcardQuery query6 = new WildcardQuery(t6);
                Term t7 = new Term("CustomField7", wildcard + keyWord + wildcard);
                WildcardQuery query7 = new WildcardQuery(t7);
                Term t8 = new Term("CustomField8", wildcard + keyWord + wildcard);
                WildcardQuery query8 = new WildcardQuery(t8);
                Term t9 = new Term("CustomField9", wildcard + keyWord + wildcard);
                WildcardQuery query9 = new WildcardQuery(t9);
                Term t10 = new Term("CustomField10", wildcard + keyWord + wildcard);
                WildcardQuery query10 = new WildcardQuery(t10);
                Term t11 = new Term("CustomField11", wildcard + keyWord + wildcard);
                WildcardQuery query11 = new WildcardQuery(t11);
                Term t12 = new Term("CustomField12", wildcard + keyWord + wildcard);
                WildcardQuery query12 = new WildcardQuery(t12);
                Term t13 = new Term("CustomField13", wildcard + keyWord + wildcard);
                WildcardQuery query13 = new WildcardQuery(t13);
                Term t14 = new Term("CustomField14", wildcard + keyWord + wildcard);
                WildcardQuery query14 = new WildcardQuery(t14);
                Term t15 = new Term("CustomField15", wildcard + keyWord + wildcard);
                WildcardQuery query15 = new WildcardQuery(t15);
                Term t16 = new Term("CustomField16", wildcard + keyWord + wildcard);
                WildcardQuery query16 = new WildcardQuery(t16);
                Term t17 = new Term("CustomField17", wildcard + keyWord + wildcard);
                WildcardQuery query17 = new WildcardQuery(t17);
                Term t18 = new Term("CustomField18", wildcard + keyWord + wildcard);
                WildcardQuery query18 = new WildcardQuery(t18);
                Term t19 = new Term("CustomField19", wildcard + keyWord + wildcard);
                WildcardQuery query19 = new WildcardQuery(t19);
                Term t20 = new Term("CustomField20", wildcard + keyWord + wildcard);
                WildcardQuery query20 = new WildcardQuery(t20);
                Term t21 = new Term("CustomField21", wildcard + keyWord + wildcard);
                WildcardQuery query21 = new WildcardQuery(t21);
                Term t22 = new Term("CustomField22", wildcard + keyWord + wildcard);
                WildcardQuery query22 = new WildcardQuery(t22);
                Term t23 = new Term("CustomField23", wildcard + keyWord + wildcard);
                WildcardQuery query23 = new WildcardQuery(t23);
                Term t24 = new Term("CustomField24", wildcard + keyWord + wildcard);
                WildcardQuery query24 = new WildcardQuery(t24);
                Term t25 = new Term("CustomField25", wildcard + keyWord + wildcard);
                WildcardQuery query25 = new WildcardQuery(t25);
                Term t26 = new Term("CustomField26", wildcard + keyWord + wildcard);
                WildcardQuery query26 = new WildcardQuery(t26);
                Term t27 = new Term("CustomField27", wildcard + keyWord + wildcard);
                WildcardQuery query27 = new WildcardQuery(t27);
                Term t28 = new Term("CustomField28", wildcard + keyWord + wildcard);
                WildcardQuery query28 = new WildcardQuery(t28);
                Term t29 = new Term("CustomField29", wildcard + keyWord + wildcard);
                WildcardQuery query29 = new WildcardQuery(t29);
                Term t30 = new Term("CustomField30", wildcard + keyWord + wildcard);
                WildcardQuery query30 = new WildcardQuery(t30);
                Term t31 = new Term("CustomField31", wildcard + keyWord + wildcard);
                WildcardQuery query31 = new WildcardQuery(t31);
                Term t32 = new Term("CustomField32", wildcard + keyWord + wildcard);
                WildcardQuery query32 = new WildcardQuery(t32);
                Term t33 = new Term("CustomField33", wildcard + keyWord + wildcard);
                WildcardQuery query33 = new WildcardQuery(t33);
                Term t34 = new Term("CustomField34", wildcard + keyWord + wildcard);
                WildcardQuery query34 = new WildcardQuery(t34);
                Term t35 = new Term("CustomField35", wildcard + keyWord + wildcard);
                WildcardQuery query35 = new WildcardQuery(t35);
                Term t36 = new Term("CustomField36", wildcard + keyWord + wildcard);
                WildcardQuery query36 = new WildcardQuery(t36);
                Term t37 = new Term("CustomField37", wildcard + keyWord + wildcard);
                WildcardQuery query37 = new WildcardQuery(t37);
                Term t38 = new Term("CustomField38", wildcard + keyWord + wildcard);
                WildcardQuery query38 = new WildcardQuery(t38);
                Term t39 = new Term("CustomField39", wildcard + keyWord + wildcard);
                WildcardQuery query39 = new WildcardQuery(t39);
                Term t40 = new Term("CustomField40", wildcard + keyWord + wildcard);
                WildcardQuery query40 = new WildcardQuery(t40);
                Term t41 = new Term("CustomField41", wildcard + keyWord + wildcard);
                WildcardQuery query41 = new WildcardQuery(t41);
                Term t42 = new Term("CustomField42", wildcard + keyWord + wildcard);
                WildcardQuery query42 = new WildcardQuery(t42);
                Term t43 = new Term("CustomField43", wildcard + keyWord + wildcard);
                WildcardQuery query43 = new WildcardQuery(t43);
                Term t44 = new Term("CustomField44", wildcard + keyWord + wildcard);
                WildcardQuery query44 = new WildcardQuery(t44);
                Term t45 = new Term("CustomField45", wildcard + keyWord + wildcard);
                WildcardQuery query45 = new WildcardQuery(t45);
                Term t46 = new Term("CustomField46", wildcard + keyWord + wildcard);
                WildcardQuery query46 = new WildcardQuery(t46);
                Term t47 = new Term("CustomField47", wildcard + keyWord + wildcard);
                WildcardQuery query47 = new WildcardQuery(t47);
                Term t48 = new Term("CustomField48", wildcard + keyWord + wildcard);
                WildcardQuery query48 = new WildcardQuery(t48);
                Term t49 = new Term("CustomField49", wildcard + keyWord + wildcard);
                WildcardQuery query49 = new WildcardQuery(t49);
                Term t50 = new Term("CustomField50", wildcard + keyWord + wildcard);
                WildcardQuery query50 = new WildcardQuery(t50);
                BooleanQuery bq0 = new BooleanQuery();
                bq0.Add(query0, Occur.SHOULD);
                bq0.Add(query1, Occur.SHOULD);
                bq0.Add(query2, Occur.SHOULD);
                bq0.Add(query3, Occur.SHOULD);
                bq0.Add(query4, Occur.SHOULD);
                bq0.Add(query5, Occur.SHOULD);
                bq0.Add(query6, Occur.SHOULD);
                bq0.Add(query7, Occur.SHOULD);
                bq0.Add(query8, Occur.SHOULD);
                bq0.Add(query9, Occur.SHOULD);
                bq0.Add(query10, Occur.SHOULD);
                bq0.Add(query11, Occur.SHOULD);
                bq0.Add(query12, Occur.SHOULD);
                bq0.Add(query13, Occur.SHOULD);
                bq0.Add(query14, Occur.SHOULD);
                bq0.Add(query15, Occur.SHOULD);
                bq0.Add(query16, Occur.SHOULD);
                bq0.Add(query17, Occur.SHOULD);
                bq0.Add(query18, Occur.SHOULD);
                bq0.Add(query19, Occur.SHOULD);
                bq0.Add(query20, Occur.SHOULD);
                bq0.Add(query21, Occur.SHOULD);
                bq0.Add(query22, Occur.SHOULD);
                bq0.Add(query23, Occur.SHOULD);
                bq0.Add(query24, Occur.SHOULD);
                bq0.Add(query25, Occur.SHOULD);
                bq0.Add(query26, Occur.SHOULD);
                bq0.Add(query27, Occur.SHOULD);
                bq0.Add(query28, Occur.SHOULD);
                bq0.Add(query29, Occur.SHOULD);
                bq0.Add(query30, Occur.SHOULD);
                bq0.Add(query31, Occur.SHOULD);
                bq0.Add(query32, Occur.SHOULD);
                bq0.Add(query33, Occur.SHOULD);
                bq0.Add(query34, Occur.SHOULD);
                bq0.Add(query35, Occur.SHOULD);
                bq0.Add(query36, Occur.SHOULD);
                bq0.Add(query37, Occur.SHOULD);
                bq0.Add(query38, Occur.SHOULD);
                bq0.Add(query39, Occur.SHOULD);
                bq0.Add(query40, Occur.SHOULD);
                bq0.Add(query41, Occur.SHOULD);
                bq0.Add(query42, Occur.SHOULD);
                bq0.Add(query43, Occur.SHOULD);
                bq0.Add(query44, Occur.SHOULD);
                bq0.Add(query45, Occur.SHOULD);
                bq0.Add(query46, Occur.SHOULD);
                bq0.Add(query47, Occur.SHOULD);
                bq0.Add(query48, Occur.SHOULD);
                bq0.Add(query49, Occur.SHOULD);
                bq0.Add(query50, Occur.SHOULD);
                System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
                sw.Start();
                
                searcher.Search(bq0, collector);//执行搜索，获取查询结果集对象

                if (collector == null || collector.TotalHits == 0)
                {
                    TotalCount = 0;
                }
                else
                {
                    int start = PageSize * (CurrentPage - 1);
                    //结束数  
                    int limit = PageSize;
                    ScoreDoc[] hits = collector.TopDocs(start, limit).ScoreDocs;
                    TotalCount = collector.TotalHits;
                    Label2.Text = TotalCount.ToString();
                }
                sw.Stop();
                Label1.Text= sw.ElapsedMilliseconds.ToString();
            }
            catch(Exception e2)
            {

            }

        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            int CurrentPage = 1;
            int PageSize = 30;
            string segPath = "F://123文档/Index";

            FSDirectory directory = null;
            StandardAnalyzer analyzer = null;
            IndexSearcher searcher = null;

            Lucene.Net.Util.Version ver = Lucene.Net.Util.Version.LUCENE_30;
            try
            {
                directory = FSDirectory.Open(new DirectoryInfo(segPath), new NativeFSLockFactory());
                analyzer = new StandardAnalyzer(ver);//StandardAnalyzer只支持英文的空格分词

                TopScoreDocCollector collector = TopScoreDocCollector.Create(CurrentPage * PageSize, false);
                searcher = new IndexSearcher(directory, true);
                string wildcard = "*";
                string keyWord = TextBox1.Text.Trim();
                Term t50 = new Term("SearchField", wildcard + keyWord + wildcard);
                WildcardQuery query50 = new WildcardQuery(t50);
                System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
                sw.Start();

                searcher.Search(query50, collector);//执行搜索，获取查询结果集对象

                if (collector == null || collector.TotalHits == 0)
                {
                    TotalCount = 0;
                }
                else
                {
                    int start = PageSize * (CurrentPage - 1);
                    //结束数  
                    int limit = PageSize;
                    ScoreDoc[] hits = collector.TopDocs(start, limit).ScoreDocs;
                    TotalCount = collector.TotalHits;
                    Label2.Text = TotalCount.ToString();
                }
                sw.Stop();
                Label1.Text = sw.ElapsedMilliseconds.ToString();
            }
            catch (Exception e2)
            {

            }
        }
    }
}
